package com.example.telesys.service.impl;

import com.example.telesys.mapper.CustomerorderMapper;
import com.example.telesys.pojo.Customerorder;
import com.example.telesys.pojo.CustomerorderExample;
import com.example.telesys.service.CustomerorderService;
import com.example.telesys.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CutomerorderServiceImpl implements CustomerorderService {

    @Autowired
    private CustomerorderMapper customerorderMapper;

    @Override
    @Transactional
    public int insertPhoneOrder(Customerorder customerorder) {
        customerorder.setDate(DateUtil.getToday());
        return customerorderMapper.insertSelective(customerorder);
    }

    @Override
    @Transactional
    public int insertBroadbandOrder(Customerorder customerorder) {
        customerorder.setDate(DateUtil.getToday());
        return customerorderMapper.insertSelective(customerorder);
    }

    @Override
    @Transactional
    public int insertTrafficOrder(Customerorder customerorder) {
        customerorder.setDate(DateUtil.getToday());
        return customerorderMapper.insertSelective(customerorder);
    }

    @Override
    public List<Customerorder> selectAllCustomerOrders() {
        return customerorderMapper.selectByExample(null);
    }

    @Override
    public List<Customerorder> selectCustomerAndNumber() {
        return customerorderMapper.selectCustomerAndNumber();
    }

    @Override
    public List<Customerorder> selectYearlyOrder(String username, String year) {
        CustomerorderExample customerorderExample = new CustomerorderExample();
        CustomerorderExample.Criteria criteria = customerorderExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andDateLike(year + "%");
        return customerorderMapper.selectByExample(customerorderExample);
    }

    @Override
    public int setOrderStatus(String role, String orderid, Integer code) {
        //根据对应的orderid 查找对应的Order
        CustomerorderExample customerorderExample = new CustomerorderExample();
        CustomerorderExample.Criteria criteria = customerorderExample.createCriteria();
        criteria.andOrderidEqualTo(orderid);
        Customerorder customerorder = customerorderMapper.selectByExample(customerorderExample).get(0);
        int orderStatus = 0;
        switch (role) {
            case "管理员":
                customerorder.setAdmincheck(code);
                orderStatus = customerorderMapper.updateByExampleSelective(customerorder, customerorderExample);
                break;
            case "价格审批员":
                customerorder.setPricecheck(code);
                orderStatus = customerorderMapper.updateByExampleSelective(customerorder, customerorderExample);
                break;
            case "风险审批员":
                customerorder.setRiskcheck(code);
                orderStatus = customerorderMapper.updateByExampleSelective(customerorder, customerorderExample);
                break;
            case "领导":
                customerorder.setLeadercheck(code);
                orderStatus = customerorderMapper.updateByExampleSelective(customerorder, customerorderExample);
                break;
            default:
                System.out.println("默认情况.");
        }
        return orderStatus;
    }

    @Override
    public List<Customerorder> selectCustomerOrdersByOrderId(String orderid) {
        CustomerorderExample customerorderExample = new CustomerorderExample();
        CustomerorderExample.Criteria criteria = customerorderExample.createCriteria();
        criteria.andOrderidLike(orderid + "%");
        return customerorderMapper.selectByExample(customerorderExample);
    }

    @Override
    public List<Customerorder> selectMyOrders(String username) {
        CustomerorderExample customerorderExample = new CustomerorderExample();
        CustomerorderExample.Criteria criteria = customerorderExample.createCriteria();
        criteria.andUsernameEqualTo(username);
        return customerorderMapper.selectByExample(customerorderExample);
    }

}
